<?php


class ChargeManager {
    public function __construct(Mypdo $db){
        $this->db= $db;
    }

    public function add(Charge $charge){
        $req = $this->db->prepare('INSERT INTO charge ( id_loc, nom_charge, montant_charge, tva_charge ) VALUES
                  (  :id_loc, :nom_charge, :montant_charge, :tva_charge)');
        $req->bindValue(':id_loc', $charge->getIdLoc(), PDO::PARAM_INT);
        $req->bindValue(':nom_charge', $charge->getNomCharge(), PDO::PARAM_STR);
        $req->bindValue(':montant_charge', $charge->getMontantCharge(), PDO::PARAM_STR);
        $req->bindValue(':tva_charge', $charge->getTvaCharge(), PDO::PARAM_STR);

        $req->execute();

    }


    public function getList(){
        $listeCharges = array();
        $sql = 'SELECT * FROM charge ORDER BY id_charge';
        $req = $this->db->query($sql);
        while ($charge = $req->fetch(PDO::FETCH_OBJ)){
            $listeCharges[] = new Message($charge);
        }
        $req->closeCursor();
        return $listeCharges;

    }

    public function  delChargeByIdPropri($id_propri){
        $sql = 'DELETE FROM immoloc.charge WHERE id_loc IN
        (SELECT l.id_loc FROM location l
        INNER JOIN immeuble i ON l.id_immeu = i.id_immeu
        WHERE id_propri = :id_propri)';
        $req = $this->db->prepare($sql);
        $req->bindValue(':id_propri', $id_propri, PDO::PARAM_INT);

        $req->execute();

    }

    public function  delChargeByIdImmeu($id_immeu){
        $sql = 'DELETE FROM immoloc.charge WHERE id_loc IN
        (SELECT id_loc FROM  location
        WHERE id_immeu = :id_immeu)';
        $req = $this->db->prepare($sql);
        $req->bindValue(':id_immeu', $id_immeu, PDO::PARAM_INT);

        $req->execute();

    }

    public function delChargeByIdLoc($id_loc)
    {
        $sql = 'DELETE FROM immoloc.charge WHERE id_loc = :id_loc';
        $req = $this->db->prepare($sql);
        $req->bindValue(':id_loc', $id_loc, PDO::PARAM_INT);

        $req->execute();
    }

    public function getListByIdLoc($id_loc)
    {
        $listeCharges = array();
        $sql = 'SELECT * FROM charge WHERE id_loc = :id_loc ORDER BY id_charge';
        $req = $this->db->prepare($sql);
        $req->bindValue(':id_loc', $id_loc, PDO::PARAM_INT);
           $req->execute();
        while ($charge = $req->fetch(PDO::FETCH_OBJ)){
            $listeCharges[] = new Charge($charge);
        }
        $req->closeCursor();
        return $listeCharges;
    }

    public function getCharge($id_charge)
    {
        $sql = 'SELECT * FROM charge WHERE id_charge = :id_charge';
        $req = $this->db->prepare($sql);
        $req->bindValue(':id_charge', $id_charge, PDO::PARAM_INT);
        $req->execute();
        return new Charge($req->fetch(PDO::FETCH_ASSOC));
    }

    public function update(Charge $charge)
    {
        $req = $this->db->prepare('UPDATE charge SET id_loc = :id_loc, nom_charge = :nom_charge, montant_charge = :montant_charge, tva_charge = :tva_charge
         WHERE id_charge = :id_charge');
        $req->bindValue(':id_charge', $charge->getIdCharge(), PDO::PARAM_INT);
        $req->bindValue(':id_loc', $charge->getIdLoc(), PDO::PARAM_INT);
        $req->bindValue(':nom_charge', $charge->getNomCharge(), PDO::PARAM_STR);
        $req->bindValue(':montant_charge', $charge->getMontantCharge(), PDO::PARAM_STR);
        $req->bindValue(':tva_charge', $charge->getTvaCharge(), PDO::PARAM_STR);

        $req->execute();
    }

    public function delCharge($id_charge)
    {
        $sql = 'DELETE FROM immoloc.charge WHERE id_charge = :id_charge';
        $req = $this->db->prepare($sql);
        $req->bindValue(':id_charge', $id_charge, PDO::PARAM_INT);

        $req->execute();
    }
} 